﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace BlueLotus7.ArchiveSystem.SinaContentApi.Weibo
{
    public static partial class ContentAnalyzer
    {
        private const string bodyStartTag = "<body>";
        private const string bodyEndTag = "</body>";
        private const string tag_src = "src";
        private const string tag_href = "href";
        private const string tag_div = "div";
        private const string tag_id = "id";

        private const string class_Comment = "c";
        private const string class_CommentToTarget = "ctt";
        private const string class_CommentTime = "ct";

        private const string html_space = "&nbsp;";

        private const string content_fromConstString = "来自";
        private const string content_replyConstString = "回复:";

        #region Utility

        /// <summary>
        /// Figure out body content
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        private static string CleanHtmlContent(string htmlContent)
        {
            string result = htmlContent;

            if (!string.IsNullOrWhiteSpace(htmlContent))
            {
                int bodyStart = htmlContent.IndexOf(bodyStartTag);
                int bodyEnd = htmlContent.IndexOf(bodyEndTag);

                if (bodyStart < bodyEnd && bodyStart > -1)
                {
                    result = htmlContent.Substring(bodyStart, bodyEnd + bodyEndTag.Length - bodyStart);
                    result = result.Replace("&nbsp;", string.Empty);
                }
            }

            return result;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static byte[] LoadBinary(Uri uri)
        {
            byte[] result = null;
            byte[] buffer = new byte[4096];

            WebRequest request = WebRequest.Create(uri);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        int count = 0;
                        do
                        {
                            count = responseStream.Read(buffer, 0, buffer.Length);
                            memoryStream.Write(buffer, 0, count);

                        } while (count != 0);

                        result = memoryStream.ToArray();
                    }
                }
            }

            return result;
        }


        private static string GetXmlAttributeValue(XmlNode xmlNode, string attributeName)
        {
            string result = string.Empty;

            if (xmlNode != null && !string.IsNullOrWhiteSpace(attributeName))
            {
                XmlAttribute attribute = xmlNode.Attributes[attributeName];
                if (attribute != null)
                {
                    result = attribute.Value == null ? string.Empty : attribute.Value;
                }
            }

            return result;
        }

        private static Dictionary<string, string> TranslateUrlParameters(string url)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            if (!string.IsNullOrWhiteSpace(url))
            {
                int index = url.IndexOf("?");
                int length = url.Length - index - 1;
                if (index > -1 && length > 0)
                {
                    url = url.Substring(index + 1, url.Length - index - 1);
                }

                string[] parameters = url.Split('&');
                foreach (string parameter in parameters)
                {
                    if (!string.IsNullOrWhiteSpace(parameter))
                    {
                        string[] keyvalue = parameter.Split('=');
                        if (keyvalue.Length == 2)
                        {
                            string key = keyvalue[0];
                            string value = keyvalue[1];

                            if (!result.ContainsKey(key))
                            {
                                result.Add(key, value);
                            }
                        }
                    }
                }
            }

            return result;
        }

        private static string GetNextPageLink(XmlNode bodyContentXml)
        {
            string link = null;

            if (bodyContentXml != null)
            {
                XmlNode pageListNode = bodyContentXml.SelectSingleNode("descendant::div[@id='pagelist']");
                if (pageListNode != null)
                {
                    XmlNode nextPageLinkNode = pageListNode.SelectSingleNode("descendant::form/div/a");
                    if (nextPageLinkNode != null)
                    {
                        link = GetXmlAttributeValue(nextPageLinkNode, tag_href);
                    }
                }
            }

            return link;
        }

        #endregion
    }
}
